package com.stark.asm.service.level.impl;

import java.util.Date;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.stark.asm.common.util.CommonConstant;
import com.stark.asm.common.util.page.Result;
import com.stark.asm.domain.level.Level;
import com.stark.asm.domain.level.query.LevelQuery;
import com.stark.asm.manager.level.LevelManager;
import com.stark.asm.service.level.LevelService;

/**
 * Level information service interface
 * @author WangYue
 *
 */
public class LevelServiceImpl implements LevelService {
    private static final Logger log = LogManager.getLogger(LevelServiceImpl.class);
	private LevelManager levelManager;
	@Override
	public Result addLevel(Level level) {
		 Result result = new Result();
		 level.setCreateTime(new Date()); //set create time
     	level.setUpdateTime(new Date()); //set update time
     	level.setYn(CommonConstant.YN_NO_DELETE); //set yn=0
	        try {
	            boolean resultFlag = levelManager.addLevel(level);
	            result.addDefaultModel("resultFlag", resultFlag);
	            result.setSuccess(true);
	        } catch (Exception e) {
	            log.error("LevelServiceImpl -> addLevel() error!!", e);
	        }
	        return result;
	}

	@Override
	public Result updateLevel(Level level) {
		 Result result = new Result();
		 level.setUpdateTime(new Date()); //set update time
	        try {
	            boolean resultFlag = levelManager.updateLevel(level);
	            result.addDefaultModel("resultFlag", resultFlag);
	            result.setSuccess(true);
	        } catch (Exception e) {
	            log.error("LevelServiceImpl -> updateLevel() error!!", e);
	        }
	        return result;
	}

	@Override
	public Result getLevelById(Long id) {
		 Result result = new Result();
	        try {
	            Level level = levelManager.getLevelById(id);
	            result.addDefaultModel("level", level);
	            result.setSuccess(true);
	        } catch (Exception e) {
	            log.error("LevelServiceImpl -> getLevelById() error!!", e);
	        }
	        return result;
	}

	@Override
	public Result getLevelByLevelCode(String levelCode) {
		 Result result = new Result();
	        try {
	            Level level = levelManager.getLevelByLevelCode(levelCode);
	            result.addDefaultModel("level", level);
	            result.setSuccess(true);
	        } catch (Exception e) {
	            log.error("LevelServiceImpl -> getLevelByLevelCode() error!!", e);
	        }
	        return result;
	}

	@Override
	public Result queryLevelListByQueryBean(LevelQuery levelQuery) {
		Result result = new Result();
        try {
            List<Level> levelList = levelManager.queryLevelListByQueryBean(levelQuery);
            result.addDefaultModel("levelList", levelList);
            result.setSuccess(true);
        } catch (Exception e) {
            log.error("LevelServiceImpl -> queryLevelListByQueryBean() error!!", e);
        }
        return result;
	}

	@Override
	public Result queryLevelListPageByQueryBean(LevelQuery levelQuery,
			int pageIndex, int pageSize) {
		Result result = new Result();
        try {
            List<Level> levelList = levelManager.queryLevelListPageByQueryBean(levelQuery,pageIndex,pageSize);
            result.addDefaultModel("levelList", levelList);
            result.setSuccess(true);
        } catch (Exception e) {
            log.error("LevelServiceImpl -> queryLevelListPageByQueryBean() error!!", e);
        }
        return result;
	}

	@Override
	public Result deleteLevelById(Long id) {
		 Result result = new Result();
	        try {
	            boolean resultFlag = levelManager.deleteLevelById(id);
	            result.addDefaultModel("resultFlag", resultFlag);
	            result.setSuccess(true);
	        } catch (Exception e) {
	            log.error("LevelServiceImpl -> deleteLevelById() error!!", e);
	        }
	        return result;
	}
	
	@Override
	public Result deleteLevelByIds(Long ids[]){
		 Result result = new Result();
	        try {
	        	if(null!=ids&&ids.length>0){
	        		boolean resultFlag = levelManager.deleteLevelByIds(ids);
	        		result.addDefaultModel("resultFlag", resultFlag);
	        		result.setSuccess(true);
	        		
	        	}else{
	        		 log.error("LevelServiceImpl -> deleteLevelById() ids param null!!");
	        	}
	        } catch (Exception e) {
	            log.error("LevelServiceImpl -> deleteLevelById() error!!", e);
	        }
	    return result;
	}


	public void setLevelManager(LevelManager levelManager) {
		this.levelManager = levelManager;
	}
	
	

}
